<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="企业名称" prop="createDeptName">
        <el-input
          v-model="queryParams.createDeptName"
          placeholder="请输入创建部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="危险物品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="危险物品形态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder="请选择危险物品形态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.dict_wastes_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上报月份" prop="monthly">
        <el-input
          v-model="queryParams.monthly"
          placeholder="请输入月份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="上报人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="createUserPhone">
        <el-input
          v-model="queryParams.createUserPhone"
          placeholder="请输入创建人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="企业名称" align="center" prop="createDeptName" />
      <el-table-column label="危险物品名称" align="center" prop="name" />
      <el-table-column label="数量" align="center" prop="num" />
      <el-table-column label="单位" align="center" prop="unit" :formatter="(row, column) => dictFormatter(row.unit, column, dict.type.dict_unit_type)"/>
      <el-table-column label="危险物品形态" align="center" prop="state" :formatter="(row, column) => dictFormatter(row.state, column, dict.type.dict_wastes_state)"/>
      <el-table-column label="上报月份" align="center" prop="monthly" />
      <el-table-column label="上报人" align="center" prop="createBy" />
      <el-table-column label="联系电话" align="center" prop="createUserPhone" />
      <el-table-column label="当前处理步骤" align="center" prop="handleStep" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['handle:log:list']"
          >处理记录</el-button>

          <el-button
            v-if="scope.row.complete!='1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handle(scope.row)"
            v-hasPermi="['wasteDisposal:info:handleWaste']"
          >处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改危险废物处置信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="危险品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="数量" prop="num">
              <el-input v-model="form.num" placeholder="请输入数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-select
                v-model="form.unit"
                placeholder="请选择单位"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.dict_unit_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="上报月份" prop="monthly">
              <el-input v-model="form.monthly" placeholder="请输入月份" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="危险物品形态" prop="state">
              <el-select
                v-model="form.state"
                placeholder="请选择危险物品形态"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.dict_wastes_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="企业名称" prop="createDeptName">
          <el-input v-model="form.createDeptName" placeholder="请输入创建部门名称" />
        </el-form-item>
        <el-form-item label="联系电话" prop="createUserPhone">
          <el-input v-model="form.createUserPhone" placeholder="请输入创建人电话" />
        </el-form-item>
        <el-table v-loading="loading" :data="logList">
          <el-table-column label="处理步骤" align="center" prop="step" />
          <el-table-column label="处理部门" align="center" prop="createDeptName" />
          <el-table-column label="处理时间" align="center" prop="createTime" />
          <el-table-column label="联系电话" align="center" prop="createUserPhone" />
        </el-table>

      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, handleWaste} from "@/api/wasteDisposal/info";
import { listLog } from "@/api/handle/log";
import { getUserProfile } from "@/api/system/user";
export default {
  name: "handle",
  dicts: ['dict_wastes_state','dict_unit_type'],
  data() {
    return {
      logList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 危险废物处置信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        num: null,
        unit: null,
        plan: null,
        state: null,
        monthly: null,
        complete: null,
        report: null,
        createDeptId: null,
        createDeptName: null,
        createBy: null,
        createUserPhone: null,
        handleStep: null,
        handleDeptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getCreateUser();
  },
  methods: {
    /** 获取当前登录人信息 */
    getCreateUser(){
      getUserProfile().then(response=>{
        //查询当前处理部门的
        this.queryParams.handleDeptId = response.data.deptId
        this.queryParams.report = '1';//已上报的
        this.queryParams.plan = '0';//实际上报
        this.queryParams.complete = '0';//未处理完成的
        this.getList();
      });
    },
    /** 查询危险废物处置信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        num: null,
        unit: null,
        plan: null,
        state: null,
        monthly: null,
        complete: null,
        report: null,
        createDeptId: null,
        createDeptName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        createUserPhone: null,
        handleStep: null,
        handleDeptId: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 处置记录 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "处置记录";
        listLog({wasteId:id}).then(response => {
          this.logList = response;
        });
      });

    },
    /** 处理 */
    handle(row) {
      this.reset();
      const id = row.id || this.ids
      handleWaste(id).then(response => {
        this.$modal.msgSuccess("处理成功");
        this.getList();
      });
    },
    /** 列表字典转换 */
    dictFormatter(row, column, dict){
      return dict.filter((item) => item.value === row)[0].label;
    }
  }
};
</script>
